package com.zbkj.service.service.impl;

import com.zbkj.common.exception.AppException;
import com.zbkj.common.model.product.Product;
import com.zbkj.common.model.traceability.Batches;
import com.zbkj.common.model.traceability.TraceabilityLogs;
import com.zbkj.common.model.traceability.TraceableItems;
import com.zbkj.common.response.traceability.TraceabilityDetailResponse;
import com.zbkj.service.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 溯源查询Service实现类
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: xiaolic
 * +----------------------------------------------------------------------
 */
@Service
public class TraceabilityServiceImpl implements TraceabilityService {

    @Autowired
    private TraceableItemsService traceableItemsService;
    
    @Autowired
    private BatchesService batchesService;
    
    @Autowired
    private TraceabilityLogsService traceabilityLogsService;
    
    @Autowired
    private ProductService productService;
    
    @Autowired
    private MerchantService merchantService;

    /**
     * 根据唯一溯源ID查询溯源详情并增加访问计数
     * @param traceId 唯一溯源ID
     * @return 溯源详情
     */
    @Override
    public TraceabilityDetailResponse queryTraceDetailByTraceId(Long traceId) {
        // 根据溯源ID查询可追溯项
        TraceableItems item = traceableItemsService.getByTraceId(traceId);
        if (item == null) {
            throw new AppException("溯源信息不存在");
        }
        
        // 增加访问计数
        traceableItemsService.incrementScanCountByTraceId(traceId);
        
        // 查询批次信息
        Batches batch = batchesService.getDetail(item.getBatchId());
        if (batch == null) {
            throw new AppException("批次信息不存在");
        }
        
        // 查询追溯日志
        List<TraceabilityLogs> logs = traceabilityLogsService.getLogsByTraceableItemIdOrBatchId(item.getId(), batch.getId());
        
        // 查询商品信息
        String productName = "";
        try {
            Product product = productService.getById(batch.getProductId());
            if (product != null) {
                productName = product.getName();
            }
        } catch (Exception e) {
            // 忽略异常，不影响主流程
        }
        
        // 查询商家信息
        String merchantName = "";
        try {
            com.zbkj.common.model.merchant.Merchant merchant = merchantService.getById(batch.getMerchantId());
            if (merchant != null) {
                merchantName = merchant.getName();
            }
        } catch (Exception e) {
            // 忽略异常，不影响主流程
        }

        // +1是因为刚刚已经增加了计数
        item.setScanCount(item.getScanCount() + 1);

        // 组装返回数据
        TraceabilityDetailResponse response = new TraceabilityDetailResponse();
        response.setBatchInfo(batch);
        response.setItemInfo(item);
        response.setProductName(productName);
        response.setMerchantName(merchantName);
        response.setLogs(logs);
        response.setScanCount(item.getScanCount());
        
        return response;
    }
}
